// Tyrghen
// Handles damages to the player to either disable friendly_fire 
// [unit, selectionName, damage, source, projectile]
private ["_debug","_unit","_selection","_damage","_source","_projectile","_current_damage","_damage_diff","_result_damage","_damage_chart","_selections","_amounts","_damage_selection","_damage_value","_idx"];

_debug = false;
_current_damage = 0;
_result_damage = _this select 2;
if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4 (%5, %6)",diag_tickTime,"handle_damage.sqf","Input",_this, player,D_player_armor];};
If (X_Client) then {
	_unit = _this select 0;
	_damage = _this select 2;
	_source = _this select 3;
	_projectile = _this select 4;
	_selection = _this select 1;

	if (isnil "D_player_armor") then {D_player_armor = 1;};
	if (isnil "D_Friendly_Fire") then {D_Friendly_Fire = True;};

	if (_unit == player) then {
		_damage_chart = player getVariable "DamageChart";
		if (isNil "_damage_chart") then {_damage_chart = [[],[]];};
		_selections = _damage_Chart select 0;
		_amounts = _damage_Chart select 1;
		if !(_selection in _selections) then {
			_selections set [count _selections, _selection];
			_amounts set [(count _selections -1), 0];
		};
		_idx = _selections find _selection;
		_current_damage = _amounts select _idx;
		_damage_diff = (_damage - _current_damage) / D_player_armor;
		if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4 (%5)",diag_tickTime,"handle_damage.sqf","Current status",_current_damage,_damage_diff];};
		if (_source != player) then {
			// Usually, this will be the case of someone shooting at you
			if ((!isnull _source) && (((side _source) getFriend d_side_player) >= 0.6)) then {
				// If it is someone from your side
				If (_projectile != "") then {
					// He did shoot at you
					if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4 (%5)",diag_tickTime,"handle_damage.sqf","Friend shooting",[_source, _projectile, _current_damage,_selection],_damage_diff];};
					if (_selection == "") then {["d_msg",[format["%1! Blue on Blue! It's %2",(name _source), (name player)],"all",objNull,"hq"]] call XNetCallEvent;};
					if (D_Friendly_Fire) then { _current_damage = _current_damage + _damage_diff; };
				} else {
					// Possibly the case of vehicles exploding, Friendly Fire doesn't apply here unless you're outside the vehicle
					if !(_source isKindOf "Man") then {
						// We have a vehicle making damages
						if (_unit in (crew _source)) then {
							// We are inside this vehicle, we take full damage
							if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4 (%5)",diag_tickTime,"handle_damage.sqf","Inside Friendly Vehicle",[_source, _projectile, _current_damage,_selection],_damage_diff];};
							_current_damage = _current_damage + _damage_diff;
						} else {
							// We are outside this vehicle, we take friendly damage, since it's a nearby friendly vec, we reduce the damage
							if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4 (%5)",diag_tickTime,"handle_damage.sqf","Outside Friendly Vehicle",[_source, _projectile, _current_damage,_selection],_damage_diff];};
							if (D_Friendly_Fire) then { _damage_diff = _damage_diff / 3; };
							_current_damage = _current_damage + _damage_diff;
						}
					} else {
						if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4 (%5)",diag_tickTime,"handle_damage.sqf","Unknown case",[_source, _projectile, _current_damage,_selection],_damage_diff];};
						_current_damage = _current_damage + _damage_diff;
					};
				};
			} else {
				// An enemy is shooting at you! (Or a nearby explosion, no matter the side)
				if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4 (%5)",diag_tickTime,"handle_damage.sqf","Enemy Shooting",[_source, _projectile, _current_damage,_selection],_damage_diff];};
				_current_damage = _current_damage + _damage_diff;
			};
		} else {
			// Blowing your own head? Joker!
			if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4 (%5)",diag_tickTime,"handle_damage.sqf","Blowing myself",[_source, _projectile, _current_damage,_selection],_damage_diff];};
			_current_damage = _current_damage + _damage_diff;
		};
		if (_idx >= 0) then {
			_amounts set [_idx, _current_damage];
			player setVariable ["DamageChart",[_selections,_amounts]];
		};
	};
	_result_damage = _current_damage;
} else {
	diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"handle_damage.sqf","Executed on the server????",_this];
};
if (_debug) then { diag_log Format["(%1)[%2]: %3 :%4  %5",diag_tickTime,"handle_damage.sqf","Resulting damage",_result_damage, [_selections,_amounts]];};
_result_damage;